package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ScoreMonthAssignAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseMonthTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ScoreMonthAssign;
import com.isunland.managesystem.entity.ScoreMonthAssignOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/16 16:27
 */
public class ScoreMonthListFragment extends BaseFragment {
  protected static final String EXTRA_PAYID =
      ScoreMonthListFragment.class.getSimpleName() + "EXTRA_PAYID";
  private int REQUEST_MONTH = 0;
  private CurrentUser mUser;
  private ExpandableListView mlv;
  private EditText etMonth;
  private String payStaffId;
  private String month;
  private ScoreMonthAssignAdapter mAdapter;
  private View mLoadingView;
  private View mErrorView;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.assignPersonalScoreList);
    mUser = CurrentUser.newInstance(getActivity());
    month = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE4);
    payStaffId = getArguments().getString(EXTRA_PAYID);
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_guide_menu, container, false);
    mlv = (ExpandableListView) view.findViewById(R.id.lv_expandable);

    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_search_friend, null, false);
    mlv.addHeaderView(header);
    etMonth = (EditText) header.findViewById(R.id.et_search_searchPersonHeader);
    TextView tvSearch = (TextView) header.findViewById(R.id.tv_search);
    etMonth.setText(month);
    etMonth.setFocusable(false);//失去焦点不可编辑
    etMonth.setClickable(true);//可点击
    etMonth.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        //按月份查询
        showDialog(new BaseMonthTimeDialogFragment(), REQUEST_MONTH);
      }
    });

    tvSearch.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        //按月份查询
        vollePostMonthScore();
      }
    });
    //等待视图
    mLoadingView = view.findViewById(R.id.ll_loading);
    //错误视图
    mErrorView = View.inflate(getActivity(), R.layout.view_error, null);

    //把这个emptyview加入到当前view hierarchy层级上
    ((ViewGroup) mlv.getParent()).addView(mErrorView);

    Button retry = (Button) mErrorView.findViewById(R.id.btn_empty);
    retry.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        vollePostMonthScore();
      }
    });
    //group不能点击收缩
    mlv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
      @Override
      public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        return true;
      }
    });
    //获取所派积分列表
    vollePostMonthScore();
    return view;
  }

  private void vollePostMonthScore() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_SCORE_MONTH_ASSIGN);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("payStaffId", payStaffId);
    params.put("regDate", month);
    //清空界面
    clearListView();
    if (MyUtils.isNetLink(getActivity())) {
      mLoadingView.setVisibility(View.VISIBLE);
      mErrorView.setVisibility(View.GONE);
    } else {
      mLoadingView.setVisibility(View.GONE);
      mErrorView.setVisibility(View.VISIBLE);
    }
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        //清空界面
        clearListView();
        mLoadingView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.VISIBLE);
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        // dialog消失
        mLoadingView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
        ScoreMonthAssignOriginal original =
            new Gson().fromJson(json, ScoreMonthAssignOriginal.class);
        if (original != null
            && original.getRows() != null
            && Base.SUCCESS == original.getResult()) {
          ArrayList<ScoreMonthAssign> mList = new ArrayList<>();
          mList.addAll(original.getRows());
          //setAdapter(mList);

          mAdapter = new ScoreMonthAssignAdapter(getActivity(), mList);
          mlv.setAdapter(mAdapter);
          for (int i = 0; i < mList.size(); i++) {
            mlv.expandGroup(i);//在分组列表视图中 默认全部展开
          }
        } else {
          ToastUtil.show("获取列表数据失败");
        }
      }
    });
  }

  private void clearListView() {
    mAdapter = new ScoreMonthAssignAdapter(getActivity(), new ArrayList<ScoreMonthAssign>());
    mlv.setAdapter(mAdapter);
  }

  protected void setAdapter(ArrayList<ScoreMonthAssign> list) {

    if (mAdapter == null) {
      mAdapter = new ScoreMonthAssignAdapter(getActivity(), list);
      mlv.setAdapter(mAdapter);
    } else {
      mAdapter.notifyDataSetChanged();
    }
    for (int i = 0; i < list.size(); i++) {
      mlv.expandGroup(i);//在分组列表视图中 默认全部展开
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_MONTH) {
      Date date = (Date) data.getSerializableExtra(BaseMonthTimeDialogFragment.EXTRA_DATE);
      month = MyDateUtil.format(date, MyDateUtil.TYPE_DATE4);
      etMonth.setText(month);
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static ScoreMonthListFragment newInstance(String payId) {
    Bundle args = new Bundle();
    args.putString(EXTRA_PAYID, payId);
    ScoreMonthListFragment fragment = new ScoreMonthListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}

